package com.vkmessenger.net.handlers;

import com.vkmessenger.net.json.LongPollResponse;

import java.util.Arrays;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: Evgeniy Krasko
 * Date: 20.06.12
 * Time: 2:42
 */
public enum LongPollResponseHandler {
    INSTANCE;

    private static Logger logger = Logger.getLogger(LongPollResponse.class.getName());

    public void handleResponse(LongPollResponse response) {
        if (response == null) return;
        for (Object[] update : response.updates) {
            switch (getIntAt(update, 0)) {
                case 0:
                    MessageResponseHandler.INSTANCE.onMessageDelete(getLongAt(update, 1));
                    break;
                case 1:
                    MessageResponseHandler.INSTANCE.onMessageFlagsChanged(getLongAt(update, 1), getIntAt(update, 2));
                    break;
                case 2:
                    MessageResponseHandler.INSTANCE.onMessageFlagsSet(getLongAt(update, 1), getIntAt(update, 2));
                    break;
                case 3:
                    MessageResponseHandler.INSTANCE.onMessageFlagsCleared(getLongAt(update, 1), getIntAt(update, 2));
                    break;
                case 4:
                    MessageResponseHandler.INSTANCE.onMessageReceived(getLongAt(update, 1),
                            getIntAt(update, 2),
                            getLongAt(update, 3),
                            getLongAt(update, 4),
                            getStringAt(update, 5),
                            getStringAt(update, 6),
                            update[7]);
                    break;
                case 8:
                    UserResponseHandler.INSTANCE.onUserOnline(-getLongAt(update, 1));
                    break;
                case 9:
                    UserResponseHandler.INSTANCE.onUserOffline(-getLongAt(update, 1), getIntAt(update, 2)); //todo check
                    break;
                case 51:
                    MessageResponseHandler.INSTANCE.onChatSettingsChanged(getLongAt(update, 1), getIntAt(update, 2) != 0); //todo check
                    break;
                case 61:
                    UserResponseHandler.INSTANCE.onUserStartTypingInDialog(getLongAt(update, 1));
                    break;
                case 62:
                    UserResponseHandler.INSTANCE.onUserStartTypingInChat(getLongAt(update, 1), getLongAt(update, 2));
                    break;
                case 70:
                    break;//IP call. Hmmmm...
                default:
                    logger.warning("Something strange from long poll:" + Arrays.toString(update));
            }
        }
    }


    private static long getLongAt(Object[] array, int index) {
        return ((Double) array[index]).longValue();
    }

    private static int getIntAt(Object[] array, int index) {
        return ((Double) array[index]).intValue();
    }

    private static String getStringAt(Object[] array, int index) {
        return array[index].toString();
    }
}
